<?php
	class newsControl extends skymvc{
		
		public function __construct(){
			parent::__construct();
		}
		
		public function onDefault(){
			$userid=M("login")->userid;
			$start=get('per_page','i');
			$limit=24;
			$where=" userid=".$userid;
			$order="newsid DESC";
			$rscount=true;
			$url="/index.php?m=news";
			$option=array(
				"where"=>$where,
				"start"=>$start,
				"limit"=>$limit,
				"order"=>$order,
				"fields"=>"newsid"
			);
			$newsids=M("news_feeds")->selectCols($option,$rscount);
			if($newsids){
				$news=M("news")->getListByIds($newsids);
				if($news){
					foreach($news as $v){
						$uids[]=$v['userid'];
					}
					$us=M("user")->getUserByIds($uids); 
					foreach($news as $k=>$v){
						if($v['imgsdata']){
							$v['imgsdata']=explode(",",$v['imgsdata']);
						}else{
							$v['imgsdata']=array();
						} 
						$v['isfollow']=1;
						$v['nickname']=$us[$v['userid']]['nickname'];
						$v['user_head']=$us[$v['userid']]['user_head'];
						$v['zans']=M("news_love")->newlist($v['newsid']);
						$v['cms']=M("news_comment")->newlist($v['newsid']);
						if($v['fromid']){
							$v['from_news']=M("news")->get($v['fromid']);
						}
						$news[$k]=$v;
					}
					$data=array();
					foreach($newsids as $newid){
						if($news[$newid]){
							$data[]=$news[$newid];
						}
						
					}	
				}
				
			}
			$per_page=$start+$limit;
			$per_page=$per_page>=$rscount?0:$per_page;
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$this->smarty->goassign(array(
				"data"=>$data,
				"pagelist"=>$pagelist,
				"per_page"=>$per_page
			));
			$tpl=M("pagetpl")->get("news","index");
			$this->smarty->display($tpl);
			$this->smarty->display("news/index.html");
		}
		
		public function onSchool(){
			$start=get('per_page','i');
			$limit=24;
			$userid=M("login")->userid;
			$where=" status=0 AND fromid=0 AND siteid=".SITEID;
			$order="newsid DESC";
			$rscount=true;
			$url="/index.php?m=news&a=school";
			$option=array(
				"where"=>$where,
				"start"=>$start,
				"limit"=>$limit,
				"order"=>$order
			);
			$data=M("news")->select($option,$rscount);
			
			if($data){
				foreach($data as $v){
					$uids[]=$v['userid'];
				}
				$us=M("user")->getUserByIds($uids); 
				//获取关注的人
				if($userid){
					$fuids=M("follow")->selectCols(array(
						"fields"=>"t_userid",
						"where"=>" userid=".$userid." AND t_userid in("._implode($uids).")"
					));
					if(!$fuids) $fuids=array($userid); 
				}
				
				foreach($data as $k=>$v){
					if($v['imgsdata']){
							$v['imgsdata']=explode(",",$v['imgsdata']);
						}else{
							$v['imgsdata']="";
					}
					if($fuids && in_array($v['userid'],$fuids)){
						$v['isfollow']=1;
					}else{
						$v['isfollow']=0;
					} 
					$v['nickname']=$us[$v['userid']]['nickname'];
					$v['user_head']=$us[$v['userid']]['user_head'];		
					$v['zans']=M("news_love")->newlist($v['newsid']);
					$v['cms']=M("news_comment")->newlist($v['newsid']);
					$data[$k]=$v;
				}
			}
			$per_page=$start+$limit;
			$per_page=$per_page>=$rscount?0:$per_page;
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$this->smarty->goassign(array(
				"data"=>$data,
				"per_page"=>$per_page,
				"pagelist"=>$pagelist
			));
			$tpl=M("pagetpl")->get("news","school");
			$this->smarty->display($tpl);
			$this->smarty->display("news/school.html");
			
		}
		
		public function onHome(){
			$userid=get('userid','i');
			if(!$userid){
				$_GET['a']='home';
				$this->onDefault();
				exit;
			}
			$start=get('per_page','i');
			$limit=24;
			$where=" status=0 AND userid=".$userid;
			$order="newsid DESC";
			$rscount=true;
			$url="/index.php?m=news&a=home";
			$option=array(
				"where"=>$where,
				"start"=>$start,
				"limit"=>$limit,
				"order"=>$order
			);
			$data=M("news")->select($option,$rscount);
			$isfollow=0;
			if(M("login")->userid){
				$f=M("follow")->selectRow("userid=".M("login")->userid." AND t_userid=".$userid);
				if($f){
					$isfollow=1;
				}
			}
			if($data){
				foreach($data as $v){
					$uids[]=$v['userid'];
				}
				$us=M("user")->getUserByIds($uids); 
				foreach($data as $k=>$v){
					if($v['imgsdata']){
							$v['imgsdata']=explode(",",$v['imgsdata']);
						}else{
							$v['imgsdata']="";
						} 
					$v['nickname']=$us[$v['userid']]['nickname'];
					$v['user_head']=$us[$v['userid']]['user_head'];	
					$v['zans']=M("news_love")->newlist($v['newsid']);
					$v['cms']=M("news_comment")->newlist($v['newsid']);
					$v['isfollow']=$isfollow;
					if($v['fromid']){
							$v['from_news']=M("news")->get($v['fromid']);
						}
					$data[$k]=$v;
				}
			}
			$per_page=$start+$limit;
			$per_page=$per_page>=$rscount?0:$per_page;
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$this->smarty->goassign(array(
				"data"=>$data,
				"user"=>M("user")->getUser($userid,$fields="userid,nickname,username,user_head,gender,follow_num,followed_num"),
				"per_page"=>$per_page,
				"pagelist"=>$pagelist
			));
			$tpl=M("pagetpl")->get("news","home");
			$this->smarty->display($tpl);
			$this->smarty->display("news/home.html");
		}
		
		public function onMy(){
			M("login")->checklogin();
			$userid=M("login")->userid;
			$start=get('per_page','i');
			$limit=24;
			$where=" status=0 AND userid=".$userid;
			$order="newsid DESC";
			$rscount=true;
			$url="/index.php?m=news&a=my";
			$option=array(
				"where"=>$where,
				"start"=>$start,
				"limit"=>$limit,
				"order"=>$order
			);
			$data=M("news")->select($option,$rscount);
			if($data){
				foreach($data as $k=>$v){
					if($v['imgsdata']){
							$v['imgsdata']=explode(",",$v['imgsdata']);
						}else{
							$v['imgsdata']="";
						} 
					$v['zans']=M("news_love")->newlist($v['newsid']);
					$v['cms']=M("news_comment")->newlist($v['newsid']);
					if($v['fromid']){
							$v['from_news']=M("news")->get($v['fromid']);
						}
					$data[$k]=$v;
				}
			}
			$per_page=$start+$limit;
			$per_page=$per_page>=$rscount?0:$per_page;
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$this->smarty->goassign(array(
				"data"=>$data,
				"per_page"=>$per_page,
				"pagelist"=>$pagelist
			));
			$tpl=M("pagetpl")->get("news","my");
			$this->smarty->display($tpl);
			$this->smarty->display("news/my.html");
		}
		
		public function onShow(){
			$newsid=get('newsid','i');
			$data=M("news")->selectRow("newsid=".$newsid);
			$user=M("user")->getUser($data['userid']);
			$this->smarty->goAssign(array(
				"data"=>$data,
				"user"=>$user
			));
			$tpl=M("pagetpl")->get("news","show");
			$this->smarty->display($tpl);
			$this->smarty->display("news/show.html");
		}
		
		public function onAdd(){
			M("login")->checklogin();
			$tpl=M("pagetpl")->get("news","add");
			$this->smarty->display($tpl);
			$this->smarty->display("news/add.html");
		}
		
		public function onSave(){
			M("login")->checklogin();
			$userid=M("login")->userid;
			//检查权限
			M("news_blacklist")->check($userid);
			
			$data=M("news")->postData();
			$data['siteid']=SITEID;
			
			$data['userid']=$userid;
			$data['dateline']=time();
			$newsid=M("news")->insert($data);
			//推送到订阅
			$us=M("follow")->selectCols(array(
				"fields"=>"userid",
				"where"=>"t_userid=".$userid,
				"limit"=>100000000
			));
		
			if(!$us) $us=array();
			$us[]=$userid;
			foreach($us as $uid){
				M("news_feeds")->insert(array(
					"userid"=>$uid,
					"newsid"=>$newsid,
					"fuserid"=>$userid,
					"dateline"=>time(),
				));
			}
			//转发通知
			if($data['fromid']){
				$news=M("news")->selectRow("newsid=".$data['fromid']);
				M("news")->changenum("spread_num",1,"newsid=".$news['newsid']);
				if($news['userid']!=$userid){
					M("notice")->add(array(
						"userid"=>$news['userid'],
						"content"=>"【转发】".$data['content']
					));
				}
			}
			$this->goAll("发布成功");
		}
		public function onDelete(){
			M("login")->checklogin();
			$newsid=get('newsid','i');
			$row=M("news")->selectRow("newsid=".$newsid);
			if(!$row || $row['userid']!=M("login")->userid){
				$this->goAll("暂无权限",1);
			}
			M("news")->update(array(
				"status"=>11
			),"newsid=".$newsid);
			//删除推送
			M("news_feeds")->delete("newsid=".$newsid);
			if($row['fromid']){
				M("news")->changenum("spread_num",-1,"newsid=".$row['fromid']);
			}
			
			$this->goAll("删除成功");
		}
		
		public function onZan(){
			M("login")->checklogin();
			$newsid=get('newsid','i');
			$userid=M("login")->userid;
			$news=M("news")->selectRow("newsid=".$newsid);
			if(!$news){
				$this->goAll("数据出错",1);
			}
			$row=M("news_love")->selectRow("newsid=".$newsid." AND userid=".$userid);
			if($row){
				M("news_love")->delete("id=".$row['id']);
				M("news")->changenum("love_num",-1,"newsid=".$newsid);
				$this->goAll("success",0,array(
					"action"=>"delete",
					"news_zan_id"=>$row['id']
				));
			}else{
				$news_zan_id=M("news_love")->insert(array(
					"newsid"=>$newsid,
					"userid"=>$userid
				));
				M("news")->changenum("love_num",1,"newsid=".$newsid);
				$user=M("login")->getUser();
				$news_zan=array(
					"id"=>$news_zan_id,
					"newsid"=>$newsid,
					"userid"=>$userid,
					"nickname"=>$user['nickname'],
					"user_head"=>images_site($user['user_head'])
				);
				$this->goAll("success",0,array(
					"action"=>"add",
					"news_zan"=>$news_zan
				));
			}
		}
		/**
		 * 处理关注事件的推送
		 */
		public function onFollowToggle(){
			$t_userid=get('t_userid','i');
			$userid=M("login")->userid;
			$row=M("follow")->selectRow("userid=".$userid." AND t_userid=".$t_userid);
			if(!$row){
				M("news_feeds")->delete("userid=".$userid." AND fuserid=".$t_userid);
			}else{
				$news=M("news")->select(array(
					"where"=>" userid=".$t_userid." AND status<11 ",
					"limit"=>50,
					"order"=>"newsid DESC",
					"fields"=>"newsid,userid,dateline"
				));
				if($news){
					foreach($news as $v){
						M("news_feeds")->insert(array(
							"userid"=>$userid,
							"fuserid"=>$v['userid'],
							"dateline"=>$v['dateline'],
							"newsid"=>$v['newsid']
						));
					}
				}
			}
			
			$this->goAll("success");
		}
	}
?>